package com.jwthhealth_pub.old.constitution.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.constitution.model.ConstitutionModel;
import com.jwthhealth_pub.old.constitution.view.adapter.QuestionnaireAdatper;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/14
 */
public class QuestionnaireActivity extends Activity {
    @BindView(R.id.rv_questionnaire)
    RecyclerView rvQuestionnaire;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.questionnaire_topbar)
    TitleLayout questionnaireTopbar;

    private RelativeLayout maleBtn, femaleBtn;

    /**
     * 0, male 1, female
     */
    private int gender;
    private int type;
    private View genderLayout;
    private QuestionnaireAdatper adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setContentView(R.layout.activity_questionnaire);
        ButterKnife.bind(this);

        maleBtn = (RelativeLayout) findViewById(R.id.btn_gender_male);
        femaleBtn = (RelativeLayout) findViewById(R.id.btn_gender_female);

        initGenderLayout();
        initClick();

    }

    private void initGenderLayout() {
        type = getIntent().getIntExtra(Constant.CONSTITUTION_TYPE, 1);
        genderLayout = findViewById(R.id.questionnaire_gender_layout);

        if (type == 5) {
            //TODO 判断是否有性别
            boolean isMale = false;
            if (isMale) {

            } else {
                maleBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        maleBtn.setBackground(getResources().getDrawable(R.drawable.btn_login_shape_purple));
                        gender = 0;
                        genderLayout.setVisibility(View.GONE);
                        initRecyclerView();
                    }
                });

                femaleBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        femaleBtn.setBackground(getResources().getDrawable(R.drawable.btn_login_shape_purple));
                        gender = 1;
                        genderLayout.setVisibility(View.GONE);
                        initRecyclerView();
                    }
                });
            }
        } else {
            genderLayout.setVisibility(View.GONE);
            initRecyclerView();
        }
    }

    private void initRecyclerView() {
        rvQuestionnaire.setLayoutManager(new LinearLayoutManager(this));
        adapter = new QuestionnaireAdatper(this, type, gender);
        rvQuestionnaire.setAdapter(adapter);
    }

    public void progressbar() {
        progressbar.setVisibility(View.VISIBLE);
    }

    private void initClick() {
        questionnaireTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        List<Integer> answers = adapter.getAnswers();
        for (int i=0; i<answers.size(); i++) {
            if (answers.get(i) == 0){
                Toast.makeText(this, getResources().getString(R.string.physicalfiness_alert), Toast.LENGTH_SHORT).show();
                return;
            }
        }
//        List answers = getAnswers();
        String ans = "";
        for (int i=0; i<answers.size(); i++){
            Log.d("QuestionnaireAdatper", "answers.get(i):" + answers.get(i));
            ans = ans + "," + answers.get(i);
        }
        ans = ans.substring(1);
        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        String id, androidtoken;
        if (userinfoModel == null){
            id = null;
            androidtoken = null;
        } else {
            id = userinfoModel.getId();
            androidtoken = userinfoModel.getAndroidtoken();
        }
        if (gender == 0){
            gender = 2;
        }
        Call<ConstitutionModel> constitutionModelCall = ApiHelper.ConstitutionModelQuestion(id, String.valueOf(gender), App.preferenceUtil.getString(Constant.MAC_ADDRESS, ""), String.valueOf(type), ans, androidtoken);
        constitutionModelCall.enqueue(new Callback<ConstitutionModel>() {
            @Override
            public void onResponse(Call<ConstitutionModel> call, Response<ConstitutionModel> response) {
                ConstitutionModel body = response.body();
                if (body.getCode().equals("0")) {
                    Intent intent = new Intent(QuestionnaireActivity.this, ConstitutionAnswerActivity.class);
                    intent.putExtra(Constant.CONSTITUTION_TYPE, type);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constant.CONSTITUTION_DATA, body);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<ConstitutionModel> call, Throwable t) {
                Log.d("QuestionnaireAdatper", "request fail");
            }
        });

        progressbar();
    }
}
